############################################################
# Through Protest to Reform: Euromaidan and Decentralization in Ukraine
# Replication Code
# This script reproduces all statistical results in the paper
# using the finalized dataset.
############################################################

# Clear environment
rm(list = ls())
options(scipen = 999)

############################################################
# Load libraries
############################################################

library(dplyr)
library(ggplot2)
library(fixest)
library(modelsummary)
library(sf)

sf_use_s2(FALSE)

############################################################
# Load finalized dataset
############################################################

# Final dataset used for regressions
protest_reform <- read.csv("replication_dataset.csv")

# Geographic boundaries of Ukrainian hromadas
hromadas <- st_read("terhromad_fin.geojson")

############################################################
# Density and Distribution Plots
############################################################

# Density: State Fund of Regional Development (DFRR)

ggplot(protest_reform, aes(x = dfrr_executed)) +
  geom_density(fill = "steelblue", alpha = 0.6) +
  labs(
    title = "",
    x = "Executed Funds in UAH",
    y = "Density"
  ) +
  theme_minimal(base_family = "Times New Roman") +
  theme(
    plot.title = element_text(family = "Times New Roman"),
    axis.title = element_text(family = "Times New Roman"),
    axis.text = element_text(family = "Times New Roman")
  )


# Histogram: E-Democracy Projects

ggplot(protest_reform, aes(x = edem_total)) +
  geom_histogram(binwidth = 1, fill = "steelblue", color = "white", boundary = 0) +
  labs(
    title = "",
    x = "Number of E-Democracy Projects",
    y = "Frequency"
  ) +
  theme_minimal(base_family = "Times New Roman") +
  theme(
    plot.title = element_text(family = "Times New Roman"),
    axis.title = element_text(family = "Times New Roman"),
    axis.text = element_text(family = "Times New Roman")
  )


############################################################
# Map: Protest and Reform Delay
############################################################

protest_reform$hromada_code <- as.character(protest_reform$hromada_code)
hromadas$COD_3 <- as.character(hromadas$COD_3)

map_data <- hromadas %>%
  left_join(protest_reform, by = c("COD_3" = "hromada_code"))

# Create points for protest locations
centroids <- st_point_on_surface(map_data)

protest_points <- centroids %>%
  filter(protest == 1)

# Map
p <- ggplot() +
  geom_sf(data = map_data, aes(fill = days_difference), color = NA) +
  geom_sf(data = protest_points, color = "black", size = 2.5) +
  scale_fill_viridis_c(option = "plasma", na.value = "grey90") +
  theme_void()

ggsave("hromada_protest_map.jpeg", p, width = 10, height = 8, dpi = 300)

############################################################
# Descriptive Figures
############################################################

# Heat plot: protests and reform delay

ggplot(protest_reform, aes(x = number_protests, y = days_difference)) +
  geom_bin2d(bins = 30) +
  scale_fill_viridis_c(option = "D") +
  theme_minimal(base_family = "Times New Roman") +
  labs(
    title = "Relationship Between Reform Delay and Number of Protests",
    x = "Number of Protests per Hromada",
    y = "Reform Delay (Days)",
    fill = "Count"
  )

############################################################
# Models: Reform Delay
############################################################

model1 <- feols(days_difference ~ protest,
                cluster = ~raion_code,
                data = protest_reform)

model2 <- feols(days_difference ~ ave_days,
                cluster = ~raion_code,
                data = protest_reform)

model3 <- feols(days_difference ~ number_protests,
                cluster = ~raion_code,
                data = protest_reform)

model4 <- feols(days_difference ~ protest + square +
                  n_settlements + incumbent +
                  distance_to_russia_belarus | raion_code,
                cluster = ~raion_code,
                data = protest_reform)

model5 <- feols(days_difference ~ ave_days + square +
                  n_settlements + incumbent +
                  distance_to_russia_belarus | raion_code,
                cluster = ~raion_code,
                data = protest_reform)

model6 <- feols(days_difference ~ number_protests + square +
                  n_settlements + incumbent +
                  distance_to_russia_belarus | raion_code,
                cluster = ~raion_code,
                data = protest_reform)

############################################################
# Models: Centrally Funded Grants
############################################################

model7 <- feols(dfrr_executed ~ protest,
                cluster = ~raion_code,
                data = protest_reform)

model8 <- feols(dfrr_executed ~ ave_days,
                cluster = ~raion_code,
                data = protest_reform)

model9 <- feols(dfrr_executed ~ number_protests,
                cluster = ~raion_code,
                data = protest_reform)

model10 <- feols(dfrr_executed ~ protest + square +
                   n_settlements + incumbent +
                   distance_to_russia_belarus | raion_code,
                 cluster = ~raion_code,
                 data = protest_reform)

model11 <- feols(dfrr_executed ~ ave_days + square +
                   n_settlements + incumbent +
                   distance_to_russia_belarus | raion_code,
                 cluster = ~raion_code,
                 data = protest_reform)

model12 <- feols(dfrr_executed ~ number_protests + square +
                   n_settlements + incumbent +
                   distance_to_russia_belarus | raion_code,
                 cluster = ~raion_code,
                 data = protest_reform)

############################################################
# Models: E-Democracy Participation
############################################################

model13 <- feols(edem_total ~ protest,
                 cluster = ~raion_code,
                 data = protest_reform)

model14 <- feols(edem_total ~ ave_days,
                 cluster = ~raion_code,
                 data = protest_reform)

model15 <- feols(edem_total ~ number_protests,
                 cluster = ~raion_code,
                 data = protest_reform)

model16 <- feols(edem_total ~ protest + square +
                   n_settlements + incumbent +
                   distance_to_russia_belarus | raion_code,
                 cluster = ~raion_code,
                 data = protest_reform)

model17 <- feols(edem_total ~ ave_days + square +
                   n_settlements + incumbent +
                   distance_to_russia_belarus | raion_code,
                 cluster = ~raion_code,
                 data = protest_reform)

model18 <- feols(edem_total ~ number_protests + square +
                   n_settlements + incumbent +
                   distance_to_russia_belarus | raion_code,
                 cluster = ~raion_code,
                 data = protest_reform)


############################################################
# Regression Tables
############################################################

modelsummary(
  list(model1, model2, model3, model4, model5, model6),
  stars = TRUE,
  output = "table_reform_delay.tex"
)

modelsummary(
  list(model7, model8, model9, model10, model11, model12),
  stars = TRUE,
  output = "table_dfrr.tex"
)

modelsummary(
  list(model13, model14, model15, model16, model17, model18),
  stars = TRUE,
  output = "table_edem.tex"
)

